package mlz.b_thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*
    共享的账户类
 */
public class Account {
    private String cardId;//卡号
    private double money;//余额   10万
    //private static double money;

    //新建lock锁对象
    private final Lock lk = new ReentrantLock();

    public Account() {
    }

    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }


/*


    //业务方法:  进行取钱操作  -> 出现了线程安全问题
    public void drawMoney(double money){
        //1.获取当前线程 -> 判断是谁获取到cpu时间片
        String name = Thread.currentThread().getName();
        //2.判断余额是否足够
        if (this.money >= money){
            System.out.println(name + "来取钱:" + money + "成功!");
            this.money -= money;
            System.out.println(name + "来取钱后,余额剩余:" + this.money);
        }else {
            System.out.println(name + "来取钱,余额不足~~");
        }
    }

*/
/*


    //同步代码块: 解决线程安全问题
    public void drawMoney(double money){
        //1.获取当前线程 -> 判断是谁获取到cpu时间片
        String name = Thread.currentThread().getName(); //小红
        //this表示共享的资源
        synchronized (this) {
            //2.判断余额是否足够
            if (this.money >= money){
                System.out.println(name + "来取钱:" + money + "成功!");
                this.money -= money;
                System.out.println(name + "来取钱后,余额剩余:" + this.money);
            }else {
                System.out.println(name + "来取钱,余额不足~~");
            }
        }
    }
*/

/*

    //普通同步方法的实现方式
    public synchronized void drawMoney(double money){
        //1.获取当前线程 -> 判断是谁获取到cpu时间片
        String name = Thread.currentThread().getName(); //小红
        //2.判断余额是否足够
        if (this.money >= money){
            System.out.println(name + "来取钱:" + money + "成功!");
            this.money -= money;
            System.out.println(name + "来取钱后,余额剩余:" + this.money);
        }else {
            System.out.println(name + "来取钱,余额不足~~");
        }
    }
*/

/*

    //静态同步代码块
    public static synchronized void drawMoney(double money){
        //1.获取当前线程 -> 判断是谁获取到cpu时间片
        String name = Thread.currentThread().getName(); //小红
        //2.判断余额是否足够
        if (Account.money >= money){
            System.out.println(name + "来取钱:" + money + "成功!");
            Account.money -= money;
            System.out.println(name + "来取钱后,余额剩余:" + Account.money);
        }else {
            System.out.println(name + "来取钱,余额不足~~");
        }
    }
*/

    //Lock锁方式实现
    public void drawMoney(double money){
        //1.获取当前线程 -> 判断是谁获取到cpu时间片
        String name = Thread.currentThread().getName();

        //加锁
        lk.lock();

        //2.判断余额是否足够
        if (this.money >= money){
            System.out.println(name + "来取钱:" + money + "成功!");
            this.money -= money;
            System.out.println(name + "来取钱后,余额剩余:" + this.money);
        }else {
            System.out.println(name + "来取钱,余额不足~~");
        }

        //释放锁
        lk.unlock();
    }




    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}
